package qwq.zyu.spigotPluginTianguo.Command;

import org.bukkit.ChatColor;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import qwq.zyu.spigotPluginTianguo.SpigotPluginTianguo;
import qwq.zyu.spigotPluginTianguo.VideoPlayer;

public class PlayTianguoCommand implements CommandExecutor {

    private final SpigotPluginTianguo plugin;
    private final VideoPlayer videoPlayer;

    public PlayTianguoCommand(SpigotPluginTianguo plugin, VideoPlayer videoPlayer) {
        this.plugin = plugin;
        this.videoPlayer = videoPlayer;
    }

    @Override
    public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
        String senderType;
        if (sender instanceof Player) {
            senderType = "Player";
        } else if (sender.getName().equals("CONSOLE")) {
            senderType = "Console";
        } else {
            senderType = "CommandBlock";
        }
        String senderName = sender.getName();

        plugin.getLogger().info("[playTianguo] Command executed by: " + senderType + " (" + senderName + ")");

        boolean enableAudio = plugin.isAudioEnabled();
        double[] pixelSizeContainer = {plugin.getPixelSize()}; // 使用数组来解决final限制

        if (args.length > 0) {
            String firstParam = args[0].toLowerCase();
            
            // 尝试解析第一个参数为像素大小
            try {
                pixelSizeContainer[0] = Double.parseDouble(args[0]);
                plugin.getLogger().info("[playTianguo] Using custom pixel size: " + pixelSizeContainer[0]);
            } catch (NumberFormatException e) {
                // 如果不是数字，则当作音频参数处理
                switch (firstParam) {
                    case "true":
                    case "1":
                    case "on":
                        enableAudio = true;
                        break;
                    case "false":
                    case "0":
                    case "off":
                        enableAudio = false;
                        break;
                }
            }
        }

        if (!plugin.isCommandStartEnabled()) {
            sender.sendMessage(ChatColor.RED + "Command-triggered playback is disabled!");
            plugin.getLogger().warning("[playTianguo] Command start is disabled.");
            return true;
        }

        if (!plugin.canPlay()) {
            double remainingTime = plugin.getRemainingCooldown();
            sender.sendMessage(ChatColor.YELLOW + "Tianguo is on cooldown!");
            sender.sendMessage(ChatColor.YELLOW + String.format("Remaining cooldown: %.2f seconds", remainingTime));
            return true;
        }

        if (enableAudio) {
            String playsoundCmd = "playsound niacl:music_disc.tententengoku_jigokukoku record @a ~ ~ ~ 10000 1.0";
            long delayTicks = plugin.getSoundDelayTicks();
            plugin.getServer().getScheduler().runTaskLater(plugin, () -> {
                plugin.getServer().dispatchCommand(plugin.getServer().getConsoleSender(), playsoundCmd);
            }, delayTicks);
        }

        String executorDisplayName = sender instanceof Player ? sender.getName() : senderType;
        plugin.getServer().broadcastMessage(ChatColor.GREEN + "[Tianguo] " + executorDisplayName + " started the show!");

        plugin.getServer().getScheduler().runTask(plugin, () -> {
            // 始终使用配置文件中的位置，不管命令执行者是谁
            videoPlayer.play(plugin.getTextPosition(), pixelSizeContainer[0]);
        });

        plugin.getLogger().info("[playTianguo] Playback started successfully.");
        return true;
    }
}
